import {NetworkError} from "@/enum/networkError";

/**
 * 获取图片主题颜色
 * @param path 图片的路径
 * @param canvasId 画布id
 */
export const getImageThemeColor = async (path: string, canvasId: string) => {
    // 获取图片后缀名
    const suffix = path.split('.').slice(-1)[0]
    // uni.getImageInfo({
    //   src: path,
    //   success: (e) => {
    //     console.log(e.path) // 在安卓app端，不管src路径怎样变化，path路径始终为第一次调用的图片路径
    //   }
    // })
    // 由于getImageInfo存在问题，所以改用base64
    return new Promise<string>((resolve, reject) => {
        // 创建canvas对象
        let that = this;
        const ctx = uni.createCanvasContext(canvasId, that);
        // 图片绘制尺寸
        const imgWidth = 300;
        const imgHeight = 150;
        // 下载图片
        uni.downloadFile({
            url: path,
            timeout:10000,
            success: (res) => {
                ctx.drawImage(res.tempFilePath, 0, 0, imgWidth, imgHeight);
                ctx.draw(false,() =>{
                    uni.canvasGetImageData({
                        canvasId: canvasId,
                        x: 0,
                        y: 0,
                        width: imgWidth,
                        height: imgHeight,
                        // fail: fail,
                        success(res) {
                            // 获取图片像素点数据
                            let data = res.data;
                            let r = 1,
                                g = 1,
                                b = 1;
                            // 获取所有像素的累加值
                            for (let row = 0; row < imgHeight; row++) {
                                for (let col = 0; col < imgWidth; col++) {
                                    if (row == 0) {
                                        r += data[imgWidth * row + col];
                                        g += data[imgWidth * row + col + 1];
                                        b += data[imgWidth * row + col + 2];
                                    } else {
                                        r += data[(imgWidth * row + col) * 4];
                                        g += data[(imgWidth * row + col) * 4 + 1];
                                        b += data[(imgWidth * row + col) * 4 + 2];
                                    }
                                }
                            }
                            // 求rgb平均值
                            r /= imgWidth * imgHeight;
                            g /= imgWidth * imgHeight;
                            b /= imgWidth * imgHeight;
                            // 四舍五入
                            r = Math.round(r);
                            g = Math.round(g);
                            b = Math.round(b);
                            let rgb = `rgb(${r},${g},${b})`;
                            resolve(rgb)
                        },
                    })
                })
            },
            fail(res) {
                reject('网络异常！请检查网络链接')
            },
            complete(){

            }
        })
    })
}

/**
 * 超时
 */
export const timeout=()=>{
    return new Promise<string>((resolve,reject)=>{
        setTimeout(()=>{
            reject(NetworkError.NETWORK_TIMEOUT)
        }
        ,10000)
    })
}
